/**
 * 部门管理初始化
 */
var Barrage = {
    id: "BarrageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Barrage.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '用户名', field: 'username', align: 'center', valign: 'middle', sortable: true},
        {title: '密码', field: 'password', align: 'center', valign: 'middle', sortable: false},
        {title: '直播间链接', field: 'url', align: 'center', valign: 'middle', sortable: false},
        {title: '发送次数', field: 'times', align: 'center', valign: 'middle', sortable: true},
        {title: '发送内容', field: 'message', align: 'center', valign: 'middle', sortable: false},
        {title: '状态', field: 'statusName', align: 'center', valign: 'middle', sortable: true}];

};

/**
 * 检查是否选中
 */
Barrage.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Barrage.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加部门
 */
Barrage.openAddBarrage = function () {
    var index = layer.open({
        type: 2,
        title: '添加弹幕配置',
        area: ['800px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/barrage/barrage_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看部门详情
 */
Barrage.openBarrageDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '弹幕配置详情',
            area: ['800px', '500px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/barrage/barrage_update/' + Barrage.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除部门
 */
Barrage.delete = function () {
    if (this.check()) {

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/barrage/delete", function () {
                Feng.success("删除成功!");
                Barrage.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("barrageId", Barrage.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否刪除该配置?", operation);
    }
};


/**
 * 启动弹幕发送任务
 */
Barrage.start = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/barrage/start", function () {
                Feng.success("启动成功!");
                Barrage.table.refresh();
            }, function (data) {
                Feng.error("启动失败!" + data.responseJSON.message + "!");
            });
            ajax.set("barrageId", Barrage.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否启动发送任务?", operation);
    }
};

/**
 * 查询部门列表
 */
Barrage.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Barrage.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Barrage.initColumn();
    var table = new BSTable(Barrage.id, "/barrage/list", defaultColunms);
    table.setPaginationType("client");
    Barrage.table = table.init();
});
